package com.demo.ssy.controller;

import java.util.ArrayList;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import ssy.TestDemo;

import com.demo.ssy.beans.MobiUserBean;
import com.demo.ssy.beans.User;
import com.demo.ssy.kit.GetRandomUtil;
import com.demo.ssy.kit.TimeUtil;
import com.demo.ssy.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

	@Resource
	private UserService userService;
	private static Logger logger = Logger.getLogger(TestDemo.class);

	@RequestMapping("test")
	public String test() {
		long start = System.currentTimeMillis();
		long endTime = TimeUtil.parseTimeStamp("2016-08-10 23:59:59",
				"yyyy-MM-dd HH:mm:ss");
		long startTime = TimeUtil.parseTimeStamp("2016-06-22 00:00:00",
				"yyyy-MM-dd HH:mm:ss");
		for (int i = 0; i < 6000; i++) {
			MobiUserBean mobiUserBean = new MobiUserBean();
			String accountString = GetRandomUtil.getTel();
			while (userService.getUserByTel(accountString) != 0) {
				System.out.println("true");
				accountString = GetRandomUtil.getTel();
			}
			mobiUserBean.setAccount(accountString);
			mobiUserBean.setName(GetRandomUtil.getChineseName());
			mobiUserBean.setNick_name(GetRandomUtil.getNickName());
			mobiUserBean.setSex(String.valueOf(GetRandomUtil.name_sex));
			mobiUserBean.setSource("7");
			mobiUserBean.setState("1");
			String register_time = TimeUtil.format(
					GetRandomUtil.getTime(startTime, endTime),
					"yyyy-MM-dd HH:mm:ss");
			mobiUserBean.setRegister_time(register_time);
			userService.insertUser(mobiUserBean);
		}
		long end = System.currentTimeMillis();

		logger.info("used time=" + (end - start) + "ms");
		return "hello";
	}

	@RequestMapping("/getAllUser")
	public String getUsers(Model model) {
		ArrayList<User> list = userService.getAllUser();
		model.addAttribute("users", list);
		if (list != null && !list.isEmpty()) {
			model.addAttribute("first", list.get(0));
		}
		return "user";
	}

}
